package people.paper.tv.activity.home.modual.rmrb.view.impl;

import android.content.Context;
import android.graphics.Color;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.skyworth.ui.api.SkyWithBGLoadingView;
import com.skyworth.util.Util;

import java.util.List;

import people.paper.tv.R;
import people.paper.tv.activity.home.HomeActivity;
import people.paper.tv.activity.home.modual.rmrb.view.data.DailyData;
import people.paper.tv.activity.home.modual.rmrb.view.data.DateListData;
import people.paper.tv.utils.ui.Common;
import people.paper.tv.utils.ui.ServerErrorLayout;

public class RMRBLayout extends FrameLayout implements View.OnKeyListener, LayoutCallback, ListViewCallBack {
    IRMRBViewCallBack viewCallBack;
    DailyImgLayout paperLayout;
    TextView date/*, layoutTitle, nextTip*/;
    DailyListView dailyListView, layoutListView;
    DatePickListView datePickLayout;
    FrameLayout lvLayout, layoutBg;
    boolean isDatePick = false;
    public boolean isBigPaper = false;
    DailyData dailyDataList;
    final int width = Util.Div(790);
    final int height = Util.Div(890);

    private FrameLayout bigPaperTouchLyt;
    private Context context;

    public void addBigPapgerTouchLyt(){
        showDataPick(false);
        bigPaperTouchLyt = new FrameLayout(context);
        addView(bigPaperTouchLyt, Util.Div(1920), Util.Div(1080));
        bigPaperTouchLyt.setClickable(true);
        bigPaperTouchLyt.setFocusable(true);
        bigPaperTouchLyt.setBackgroundColor(Color.TRANSPARENT);
        bigPaperTouchLyt.bringToFront();
        bigPaperTouchLyt.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                return paperLayout.bigTouchEvent(event);
            }
        });
    }

    public void removeBigPapgerTouchLyt(){
        removeView(bigPaperTouchLyt);
    }

    public RMRBLayout(@NonNull Context context) {
        super(context);
        this.context = context;
        initView(context);
        setLoadOrErrorStatus(false);
        showLoading();
    }

    private void initView(@NonNull Context context) {
        FrameLayout contentLayout = new FrameLayout(context);
        addView(contentLayout, Util.Div(1920), Util.Div(1080));

        paperLayout = new DailyImgLayout(context);
        LayoutParams params = new LayoutParams(width, height);
        params.leftMargin = Util.Div(87);
        params.topMargin = Util.Div(160);
        addView(paperLayout, params);
        paperLayout.setOnKeyListener(this);
        paperLayout.setRMRBLayout(this);
        paperLayout.setClipChildren(false);
        paperLayout.setClipToPadding(false);

        date = new TextView(context);
        date.setTextColor(getResources().getColor(R.color.title_focus));
        date.setTextSize(Util.Dpi(36));
        LayoutParams date_params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        date_params.leftMargin = Util.Div(927);
        date_params.topMargin = Util.Div(160);
        addView(date, date_params);

       /* layoutTitle = new TextView(context);
        layoutTitle.setTextColor(getResources().getColor(R.color.title_focus));
        layoutTitle.setTextSize(Util.Dpi(32));
        LayoutParams layout_params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        layout_params.leftMargin = Util.Div(1197);
        layout_params.topMargin = Util.Div(244);
        addView(layoutTitle, layout_params);

        nextTip = new TextView(context);
        nextTip.setTextColor(getResources().getColor(R.color.title_unfocus));
        nextTip.setTextSize(Util.Dpi(28));
        LayoutParams next_params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        next_params.leftMargin = Util.Div(1400);
        next_params.topMargin = Util.Div(1030);
        addView(nextTip, next_params);*/

        layoutBg = new FrameLayout(context);
        layoutBg.setBackgroundColor(getResources().getColor(R.color.video_list_bg));
        LayoutParams bg_params = new LayoutParams(Util.Div(270), Util.Div(840));
        bg_params.leftMargin = Util.Div(927);
        bg_params.topMargin = Util.Div(210);
        addView(layoutBg, bg_params);
        layoutListView = new DailyListView(getContext(), true);
        layoutBg.addView(layoutListView);
        layoutListView.setOnKeyListener(this);
        layoutListView.setLayoutCallback(this);


        lvLayout = new FrameLayout(context);
        lvLayout.setBackgroundColor(getResources().getColor(R.color.video_bg));
        LayoutParams lv_params = new LayoutParams(Util.Div(636), Util.Div(840));
        lv_params.leftMargin = Util.Div(1197);
        lv_params.topMargin = Util.Div(210);
        addView(lvLayout, lv_params);

        dailyListView = new DailyListView(getContext(), false);
        lvLayout.addView(dailyListView);
        dailyListView.setOnKeyListener(this);
        dailyListView.setLayoutCallback(this);


        datePickLayout = new DatePickListView(context);
        addView(datePickLayout, new LayoutParams(Util.Div(394), LayoutParams.MATCH_PARENT));
        datePickLayout.setParentView(this);
        datePickLayout.setVisibility(GONE);

        paperLayout.bringToFront();
        datePickLayout.bringToFront();
    }

    public void setLoadOrErrorStatus(boolean hasData) {
        if (hasData) {
            paperLayout.setVisibility(VISIBLE);
            date.setVisibility(VISIBLE);
//            layoutTitle.setVisibility(VISIBLE);
//            nextTip.setVisibility(VISIBLE);
            layoutBg.setVisibility(VISIBLE);
            lvLayout.setVisibility(VISIBLE);
        } else {
            paperLayout.setVisibility(INVISIBLE);
            date.setVisibility(INVISIBLE);
//            layoutTitle.setVisibility(INVISIBLE);
//            nextTip.setVisibility(INVISIBLE);
            layoutBg.setVisibility(INVISIBLE);
            lvLayout.setVisibility(INVISIBLE);
            datePickLayout.setVisibility(INVISIBLE);
        }
    }

    public void setParentViewCallback(IRMRBViewCallBack view) {
        viewCallBack = view;
        dailyListView.setParentViewCallback(view);
        layoutListView.setParentViewCallback(view);
    }

    public void refreshDaily(DailyData datas) {
        setLoadOrErrorStatus(true);
        hideLoading();
        dailyDataList = datas;
        date.setText(datas.date);
//        layoutTitle.setText(datas.layoutDatas.get(0).layoutTitle);
//        nextTip.setText("查看下一版:" + datas.layoutDatas.get(1).layoutTitle);
        paperLayout.refresh(datas.layoutDatas.get(0));
        layoutListView.refresh(datas);
        dailyListView.refresh(datas);
    }

    public void refreshCurPlayId(int layoutid, int position) {
        layoutListView.refreshCurLayoutId(layoutid);
        dailyListView.refreshCurPlayId(layoutid, position);
    }

    public void refreshCurPlayLayoutId(int position) {
//        layoutListView.refreshCurLayoutId(position);
        dailyListView.refreshCurLayout(position);
    }

    public void refreshDate(List<DateListData> dateList) {
        datePickLayout.refresh(dateList);
    }

    public void showDataPick(boolean isShow) {
        if(isDatePick == isShow)
            return;
        isDatePick = isShow;
        if(isDatePick){
            datePickLayout.setVisibility(VISIBLE);
            datePickLayout.getFocus();
        }else {
            isDatePick = false;
            paperLayout.requestFocus();
            datePickLayout.setVisibility(GONE);
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            if (!isBigPaper && keyCode == KeyEvent.KEYCODE_MENU) {
                showDataPick(true);
                return true;
            } else if (isDatePick && (keyCode == KeyEvent.KEYCODE_BACK || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT)) {
                showDataPick(false);
                return true;
            } else if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT && v.equals(paperLayout)) {
                viewCallBack.preFragment();
                return true;
            } else if (isBigPaper && keyCode == KeyEvent.KEYCODE_BACK) {
                removeBigPapgerTouchLyt();
                paperLayout.setViewFullScreen(false);
                return true;
            } else if (v.equals(paperLayout) && keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                layoutListView.setPlaceFocus();
                return true;
            } else if(v.equals(paperLayout) && keyCode == KeyEvent.KEYCODE_DPAD_UP){
                viewCallBack.changeFocus();
                return true;
            } else if(v.equals(paperLayout) && keyCode == KeyEvent.KEYCODE_DPAD_DOWN){
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean onListBoundaryKey(boolean isLayout, int keycode) {
        if(keycode == KeyEvent.KEYCODE_DPAD_RIGHT){
            dailyListView.setPlaceFocus();
        }else if(keycode == KeyEvent.KEYCODE_DPAD_LEFT){
            layoutListView.setPlaceFocus();
        }
        return true;
    }

    @Override
    public void onListClick(boolean isLayout) {
        if(isLayout){
            dailyListView.refreshAdapter();
        }
    }

    public void setListVisible(boolean visible){
        dailyListView.setVisibility(visible ? VISIBLE : INVISIBLE);
        layoutListView.setVisibility(visible ? VISIBLE : INVISIBLE);
    }

    @Override
    public void refreshLayoutTitle(int layoutIndex) {
        paperLayout.refresh(dailyDataList.layoutDatas.get(layoutIndex));
        refreshCurPlayLayoutId(layoutIndex);
//        layoutTitle.setText(dailyDataList.layoutDatas.get(i).layoutTitle);
//        i++;
        /*if (dailyDataList.layoutDatas.size() > i) {
            nextTip.setText("查看下一版:" + dailyDataList.layoutDatas.get(i).layoutTitle);
        } else {
            nextTip.setVisibility(GONE);
        }*/
    }

    public boolean changeFocus(boolean isLeft) {
        if(serverErrorLayout != null && serverErrorLayout.getVisibility() == VISIBLE){
            serverErrorLayout.setFocus();
            return true;
        }
        if(!isLeft && dailyListView != null && dailyListView.getChild() > 0){
            return dailyListView.changeFocus();
        }else{
            paperLayout.requestFocus();
            return true;
        }
    }

    SkyWithBGLoadingView loadingView;

    public void showLoading() {
        if (loadingView == null) {
            loadingView = new SkyWithBGLoadingView(getContext());
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            addView(loadingView, params);
        }
        loadingView.setVisibility(View.VISIBLE);
        loadingView.showLoading();
        loadingView.bringToFront();
        if (loadingView.getParent() != null)
            loadingView.getParent().requestLayout();
    }

    public void hideLoading() {
        if (loadingView != null) {
            loadingView.dismissLoading();
            removeView(loadingView);
            loadingView = null;
        }
    }

    ServerErrorLayout serverErrorLayout;

    public void showError() {
        if (serverErrorLayout == null) {
            serverErrorLayout = new ServerErrorLayout(getContext(), reloadCallback);
            LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            addView(serverErrorLayout, params);
        }
        serverErrorLayout.setVisibility(View.VISIBLE);
        serverErrorLayout.bringToFront();
        if (serverErrorLayout.getParent() != null)
            serverErrorLayout.getParent().requestLayout();
    }

    public void hideError() {
        if (serverErrorLayout != null) {
            removeView(serverErrorLayout);
            serverErrorLayout = null;
        }
    }

    ServerErrorLayout.ReloadCallback reloadCallback = new ServerErrorLayout.ReloadCallback() {
        @Override
        public void reload() {
            viewCallBack.changeFocus();
            hideError();
            showLoading();
            viewCallBack.getNextPageData(-1);
        }

        @Override
        public boolean onkeyListener(View v, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_DPAD_LEFT) {
                    viewCallBack.preFragment();
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_RIGHT) {
                    viewCallBack.nextFragment();
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_UP) {
                    viewCallBack.changeFocus();
                    return true;
                } else if (keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_MENU) {
                    return true;
                }
            }
            return false;
        }
    };

    public void resume() {
        if(Common.CurTab == HomeActivity.RMRB_INDEX && paperLayout != null){
            paperLayout.resume();
        }
    }

    public void pause() {
        if(Common.CurTab == HomeActivity.RMRB_INDEX && paperLayout != null){
            paperLayout.pause();
        }
    }

    public void destroy() {
        if(paperLayout != null)
            paperLayout.destroy();
    }

}
